package com.woniuxy.joylifeshop.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.ShopOrderItem;
import com.woniuxy.joylifeshop.service.OrderManagementService;
import com.woniuxy.joylifeshop.vo.ViewObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
public class OrderManagementController {

    @Autowired
    OrderManagementService orderManagementService;


    @RequestMapping("initOrderByCondition")
    public ViewObject initOrderByCondition(@RequestBody HashMap<String,String> information){
        System.out.println(information);
        //从json中获取数据
        String jsonstr = information.get("jsonstr");
        ShopOrderItem shopOrderItem = JSON.parseObject(jsonstr, ShopOrderItem.class);
        System.out.println(shopOrderItem);
        //封装VO
        ViewObject viewObject = new ViewObject();
        //分页
        PageHelper.startPage(Integer.parseInt(information.get("page")),Integer.parseInt(information.get("limit")));
        //找到要查询的对象
        List<ShopOrderItem> shopOrderItems = orderManagementService.readOrderItemByCondition(shopOrderItem);
        //分页2
        PageInfo pageInfo = new PageInfo(shopOrderItems);
        List<PageInfo> pageInfos = new ArrayList<>();
        pageInfos.add(pageInfo);
        //配置vo
        viewObject.setData(pageInfos);
        viewObject.setCode(0);
        viewObject.setCount(new Long(pageInfo.getTotal()).intValue());
        viewObject.setMsg("查询成功");

        return viewObject;
    }




}
